/**
2	 * Copyright (C) 2011 JogAmp Community. All rights reserved.
3	 *
4	 * Permission is hereby granted, free of charge, to any person obtaining a
5	 * copy of this software and associated documentation files (the "Software"),
6	 * to deal in the Software without restriction, including without limitation
7	 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8	 * and/or sell copies of the Software, and to permit persons to whom the
9	 * Software is furnished to do so, subject to the following conditions:
10	 *
11	 * The above copyright notice and this permission notice shall be included
12	 * in all copies or substantial portions of the Software.
13	 *
14	 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15	 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16	 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17	 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18	 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19	 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20	 */
21	package com.jogamp.opengl.test.junit.jogl.demos.es2;
22	
23	import java.nio.FloatBuffer;
24	
25	import javax.media.opengl.GL;
26	import javax.media.opengl.GL2ES2;
27	import javax.media.opengl.GLUniformData;
28	
29	
30	import com.jogamp.opengl.test.junit.jogl.demos.GearsObject;
31	import com.jogamp.opengl.util.GLArrayDataServer;
32	import com.jogamp.opengl.util.PMVMatrix;
33	import com.jogamp.opengl.util.glsl.ShaderState;
34	
35	/**
36	 * GearsObjectES2.java <BR>
37	 * @author Brian Paul (converted to Java by Ron Cemer and Sven Gothel) <P>
38	 */
39	public class GearsObjectES2 extends GearsObject {
40	    PMVMatrix pmvMatrix;
41	    GLUniformData pmvMatrixUniform;
42	    GLUniformData colorUniform;
43	   
44	    public GearsObjectES2(float inner_radius, float outer_radius, float width,
45	                          int teeth, float tooth_depth,
46	                          PMVMatrix pmvMatrix,
47	                          GLUniformData pmvMatrixUniform,
48	                          GLUniformData colorUniform)
49	    {
50	        super(inner_radius, outer_radius, width, teeth, tooth_depth);
51	        this.pmvMatrix = pmvMatrix;
52	        this.pmvMatrixUniform = pmvMatrixUniform;
53	        this.colorUniform = colorUniform;
54	    }
55	
56	    public GearsObjectES2(GearsObject shared,
57	                          PMVMatrix pmvMatrix,
58	                          GLUniformData pmvMatrixUniform,
59	                          GLUniformData colorUniform)
60	    {
61	        super(shared);
62	        this.pmvMatrix = pmvMatrix;
63	        this.pmvMatrixUniform = pmvMatrixUniform;
64	        this.colorUniform = colorUniform;
65	    }
66	
67	    @Override
68	    public GLArrayDataServer createInterleaved(int comps, int dataType, boolean normalized, int initialSize, int vboUsage) {
69	        return GLArrayDataServer.createGLSLInterleaved(comps, dataType, normalized, initialSize, vboUsage);
70	    }
71	   
72	    @Override
73	    public void addInterleavedVertexAndNormalArrays(GLArrayDataServer array,
74	            int components) {
75	        array.addGLSLSubArray("vertices", 3, GL.GL_ARRAY_BUFFER);
76	        array.addGLSLSubArray("normals", 3, GL.GL_ARRAY_BUFFER);
77	    }
78	
79	    private void draw(GL2ES2 gl, GLArrayDataServer array, int mode) {
80	        array.enableBuffer(gl, true);
81	        gl.glDrawArrays(mode, 0, array.getElementCount());
82	        array.enableBuffer(gl, false);
83	    }
84	
85	    @Override
86	    public void draw(GL _gl, float x, float y, float angle, FloatBuffer color) {
87	        final GL2ES2 gl = _gl.getGL2ES2();
88	        final ShaderState st = ShaderState.getShaderState(gl);
89	        pmvMatrix.glPushMatrix();
90	        pmvMatrix.glTranslatef(x, y, 0f);
91	        pmvMatrix.glRotatef(angle, 0f, 0f, 1f);
92	        pmvMatrix.update();
93	        st.uniform(gl, pmvMatrixUniform);
94	
95	        colorUniform.setData(color);
96	        st.uniform(gl, colorUniform);
97	
98	        draw(gl, frontFace, GL.GL_TRIANGLE_STRIP);
99	        draw(gl, frontSide, GL.GL_TRIANGLES);
100	        draw(gl, backFace, GL.GL_TRIANGLE_STRIP);
101	        draw(gl, backSide, GL.GL_TRIANGLES);
102	        draw(gl, outwardFace, GL.GL_TRIANGLE_STRIP);
103	        draw(gl, insideRadiusCyl, GL.GL_TRIANGLE_STRIP);
104	       
105	        pmvMatrix.glPopMatrix();
106	    }   
107	}